from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
import base64
import time
import os
from app01.utils.get_city import get_city, get_ip


# Create your views here.
def index1(request):
    # 正常上传到 static目录
    img_list = os.listdir('static/upload')
    ip = get_ip(request)
    print(ip, get_city(ip))
    return render(request, 'index1.html', locals())


@csrf_exempt
def upload_image_by_paste(request):
    from app01.utils.upload_qiniu import upload_data
    img = request.POST.get('image')
    ines = img.split('base64,')
    imgData = base64.b64decode(ines[1])
    file_url = upload_data(imgData)
    # timestamp = str(int(time.time()))
    # file_url = 'static/upload/%s.%s' % (timestamp, 'jpg')
    # leniyimg = open(file_url, 'wb')
    # leniyimg.write(imgData)
    # leniyimg.close()
    return JsonResponse({'ret': 0, 'msg': 'success', 'store_path': file_url})


@csrf_exempt
def delete_photo(request):
    name = request.POST.get('name')
    import os
    os.remove(f'static/upload/{name}')
    return JsonResponse({})


# 上传到数据库
def index2(request):
    from app01.models import Cover
    cover_list = Cover.objects.filter(state=0)
    return render(request, 'index2.html', locals())


@csrf_exempt
def upload_image_by_media(request):
    from django.core.files.uploadedfile import InMemoryUploadedFile
    from io import BytesIO
    from app01 import models

    img = request.POST.get('image')
    res = img.split(',')[1]
    img_b64decode = base64.b64decode(res)

    # base64转file对象
    image = BytesIO(img_b64decode)

    file = InMemoryUploadedFile(
        file=image, field_name='file', name='图片粘贴.png', content_type='image/png', size='738096',
        charset=None)
    cover_obj = models.Cover.objects.create(url=file, state=0)

    return JsonResponse({'ret': 0, 'msg': 'success', "store_path": cover_obj.url.url})


@csrf_exempt
def delete_photo_pro(request, nid):
    from app01.models import download_delete
    from app01.models import Cover

    query = Cover.objects.filter(nid=nid)
    download_delete(query.first())
    query.delete()

    return JsonResponse({})


# 拖拽上传
def up_drag_drop(request):
    from app01.models import Cover
    cover_list = Cover.objects.filter(state=1)
    return render(request, 'index3.html', locals())


@csrf_exempt
def element_upload(request):
    from app01 import models
    file = request.FILES.get('file')
    cover_obj = models.Cover.objects.create(url=file)
    return JsonResponse({})


# 粘贴图片 url上传
def paste_url(request):
    from app01.models import Cover
    cover_list = Cover.objects.filter(state=2)
    return render(request, 'index4.html', locals())


@csrf_exempt
def upload_photo_paste(request):
    import requests
    url = request.POST.get('url')
    res = {
        "ret": 414,
        "msg": '上传成功',
        "store_path": ''
    }
    try:
        data = requests.get(url=url).content
    except:
        res['msg'] = '图片地址错误'
        return JsonResponse(res)

    from django.core.files.uploadedfile import InMemoryUploadedFile
    from io import BytesIO
    from app01 import models

    # base64转file对象
    image = BytesIO(data)

    file = InMemoryUploadedFile(
        file=image, field_name='file', name='粘贴url.png', content_type='image/png', size='738096',
        charset=None)
    cover_obj = models.Cover.objects.create(url=file, state=2)
    res['store_path'] = cover_obj.url.url
    res['ret'] = 0
    return JsonResponse(res)
